package kr.saeildadim.androidexam_saeildamil.chapter13;

import java.security.PublicKey;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

import kr.saeildadim.androidexam_saeildamil.R;
import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.widget.TextView;

public class Ch13_DateTime extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch13_datetime);
		
		Refresh();
	}
	public void mOnClick(View v){
		switch(v.getId()){
		case R.id.ch13_datetime_btn:
			Refresh();
			break;
		}
	}
	
	
	private void Refresh() {
		StringBuilder time = new StringBuilder();
		
		long epoch = System.currentTimeMillis();
		time.append("epoch : "+epoch +"\n");
		
		Calendar cal = new GregorianCalendar();
		time.append("Now : "+ String.format("%d년 %d월 %d일 %d시 %d분\n", 
				cal.get(Calendar.YEAR),
				cal.get(Calendar.MONTH)+1,
				cal.get(Calendar.DAY_OF_MONTH),
				cal.get(Calendar.HOUR_OF_DAY),
				cal.get(Calendar.MINUTE)));
		Date now = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd hh:mm:ss");
		time.append("Now : "+sdf.format(now)+"\n");
		
		Calendar tom = new GregorianCalendar();
		tom.add(Calendar.DAY_OF_MONTH, 1);
		Date tomdate = tom.getTime();
		SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy.MM.dd");
		time.append("tomorrow : "+sdf2.format(tomdate)+"\n");
		
		time.append("boot : "+Uptime(SystemClock.elapsedRealtime()));
		time.append("run : "+Uptime(SystemClock.uptimeMillis()));
		time.append("thread : "+Uptime(SystemClock.currentThreadTimeMillis()));
		
		TextView result = (TextView) findViewById(R.id.ch13_datetime);
		result.setText(time.toString());
	}


	private String Uptime(long msec) {
		long sec = msec/1000;
		String result;
		result = String.format("%d일%d시%d분%d초\n", sec/86400, sec/3600%24,
				sec/60%60, sec%60);
		return result;
	}
}
